package com.fc.advbox.service.impl;

import com.fc.advbox.dao.OrgAcctDao;
import com.fc.advbox.dao.OrgDao;
import com.fc.advbox.entity.AdvOrgAcctEntity;
import com.fc.advbox.entity.AdvOrgEntity;
import com.fc.advbox.service.IContractService;
import com.fc.advbox.service.IOrgService;
import com.fc.advbox.utils.Constant;
import com.fc.advbox.utils.Query;
import com.fc.advbox.utils.RRException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.*;

/**
 * OrgServiceImpl
 *
 * @author Gary.Jiang
 * @date 2018/6/25.13:32
 */
@Service("orgService")
@Transactional
@Slf4j
public class OrgServiceImpl implements IOrgService {

    //默认机构名称,默认管理公司
    public static final String DEFAULT_ORG_NAME = "国鑫智能";

    @Autowired
    private OrgDao orgDao;

    @Autowired
    private OrgAcctDao orgAcctDao;

    @Override
    public AdvOrgEntity getDefaultOrg(){
        Map<String, Object> params = new HashMap<>();
        params.put("name", DEFAULT_ORG_NAME);
        List<AdvOrgEntity>  list = orgDao.queryList(params);
        if(list != null && list.size() > 0){
            return list.get(0);
        }else{
            log.warn("Not found the default Org {}", DEFAULT_ORG_NAME );
            throw new RRException(String.format("默认机构 %s 不存在!", DEFAULT_ORG_NAME));
        }
    }

    @Override
    public String getOrgNameById(Long id) {
        if(id != null)
        {
            AdvOrgEntity orgEntity = orgDao.queryObject(id);
            if(orgEntity != null)
            {
                return orgEntity.getName();
            }
        }
        return null;
    }

    @Override
    public List<AdvOrgEntity> queryList(Map<String, Object> params) {
        return orgDao.queryList(params);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return orgDao.queryTotal(map);
    }

    public int queryTotal(Query query) {
        return orgDao.queryTotal(query);
    }

    @Override
    public AdvOrgEntity queryObject(Long id) {
        return orgDao.queryOrgAndAcct(id);
    }

    @Override
    @Transactional
    public void save(AdvOrgEntity orgEntity) {
        boolean newOrg = false;
        if(orgEntity.getId() != null && orgEntity.getId() > 0){
            newOrg = true;
        }
        orgDao.save(orgEntity);

        //Create org acct
        if(newOrg && orgEntity.getId() != null){
            AdvOrgAcctEntity acctEntity = new AdvOrgAcctEntity();
            acctEntity.setAcctNum(""+acctEntity.hashCode());
            acctEntity.setBalance(new BigDecimal(0));
            acctEntity.setBankName("");
            acctEntity.setCreateTime(new Date());
            acctEntity.setOrgId(orgEntity.getId());
            acctEntity.setProfit(new BigDecimal(0));
            acctEntity.setStatus(Constant.Status.ENABLE.getValue());
            orgAcctDao.save(acctEntity);
        }
    }

    @Override
    public void update(AdvOrgEntity orgEntity) {
        orgDao.update(orgEntity);
    }

    @Override
    public void delete(Long id) {
        orgDao.delete(id);
    }
}
